package notepad;

public class SimpleNotepad implements INotepad 
{
	protected Page[] pages;
	
	SimpleNotepad(int numberOfPages)
	{
		if(numberOfPages > 0)
		{
			pages = new Page[numberOfPages];
			for (int i = 0; i < pages.length; i++) 
			{
				pages[i] = new Page(String.valueOf(i + 1));
			}
		}
		else
		{
			System.out.println("Wrong input");
			System.exit(0);
		}
		
	}
	@Override
	public void addSomeText(int pageNumber, String someText) 
	{
		if(validatePage(pageNumber))
		{
			pages[pageNumber - 1].addSomeText(someText);
		}
	}

	protected boolean validatePage(int pageNumber)
	{
		return pageNumber > 0 && pageNumber <= pages.length;
	}
	
	@Override
	public void replaceText(int pageNumber, String someText) 
	{
		if(validatePage(pageNumber))
		{
			pages[pageNumber - 1].deleteText();
			pages[pageNumber - 1].addSomeText(someText);
		}
	}
	
	@Override
	public void deleteSomeText(Page selectedPage) 
	{
		
			for (int i = 0; i < pages.length; i++) 
			{
				if(pages[i].equals(selectedPage))
				{
					selectedPage.deleteText();
				}		
			}
	}

	@Override
	public void notepadInfo() 
	{
		System.out.println("Notepad info");
		for (int i = 0; i < pages.length; i++) 
		{
			if(pages[i] != null)
			{
				pages[i].showPageInfo();
			}
		}
	}
	
}
